package cn.lsh.comparator;

import cn.lsh.writable.Weather;
import org.apache.hadoop.io.RawComparator;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * 分组比较器，对排序后的数据进行分组，同一组的数据执行一次reduce
 *
 * @author 52879
 */
public class WeatherGroupComparator extends WritableComparator {

	public WeatherGroupComparator() {
		super(Weather.class, true);
	}

	@Override
	public int compare(WritableComparable a, WritableComparable b) {
		//根据年月分组
		Weather wa = (Weather) a;
		Weather wb = (Weather) b;
		int i = Integer.compare(wa.getYear(), wb.getYear());
		if (i == 0) {
			return Integer.compare(wa.getMonth(), wb.getMonth());
		}
		return i;
	}
}
